<?php
/*
 * @o: ========================================================
 * @Created By: VsCode
 * @Author: Fu
 * @name: 管理员控制器
 * @LastEditTime: 2021-05-13 13:50:47
 * @Date: 2021-04-02 18:13:40
 * @u: ========================================================
 */

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
/**
 * 首页接口
 */
class My extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $this->success('请求成功');
    }

    public function addresslist()
    {
        $param = input('param.');
        $list = Db::name('address')->where('user_id',$param['user_id'])->select();
        $this->success('成功',$list);
    }

    /**
     * @FunctionName: 
     * @Author: Fcy
     * @param {*}
     * @return {*}
     * @Date: 2021-05-13 13:44:56
     */
    public function add_address()
    {
        $param = input('param.');
        if($param['status'] == 1){
            $list = Db::name('address')->where('user_id',$param['user_id'])->select();
            foreach($list as $key=>$val){
                Db::name('address')->where('id',$val['id'])->update(['status'=>0]);
            }
        }
        $res = Db::name('address')->insert([
            'tel' =>$param['tel'],
            'username' =>$param['username'],
            'area' =>$param['area'],
            'street' =>$param['street'],
            'address' =>$param['address'],
            'status' => $param['status'],
            'user_id' => $param['user_id'],
        ]);
        
        $this->success('成功',$res);
    }


    public function edit_address()
    {
        $param = input('param.');
        if($param['status'] == 1){
            $list = Db::name('address')->where('user_id',$param['user_id'])->select();
            foreach($list as $key=>$val){
                Db::name('address')->where('id',$val['id'])->update(['status'=>0]);
            }
        }
        $res = Db::name('address')->where('id',$param['id'])->update([
            'tel' =>$param['tel'],
            'username' =>$param['username'],
            'area' =>$param['area'],
            'street' =>$param['street'],
            'address' =>$param['address'],
            'status' => $param['status'],
            'user_id' => $param['user_id'],
        ]);
        $this->success('成功',$res);
    }

    public function edit()
    {
        $param = input('param.');
        $list = Db::name('address')->where('id',$param['id'])->find();
        $this->success('成功',$list);
    }
}
